package com.example.task2u;


import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class SwipeDetector implements View.OnTouchListener {

    public static enum Action {
        LR, // Left to Right
        RL, // Right to Left
        TB, // Top to bottom
        BT, // Bottom to Top
        None // when no action was detected
    }

   // private static final String logTag = "SwipeDetector";
    private static final int MIN_DISTANCE = 100;
    private float downX, downY, upX, upY;
    private static Action mSwipeDetected = Action.None;

    public static  boolean swipeDetected() {
        return mSwipeDetected != Action.None;
    }

    public static  Action getAction() {
        return mSwipeDetected;
    }

    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            downY = event.getY();
            mSwipeDetected = Action.None;
    		Log.d("Foi no","None");

            return false; // allow other events like Click to be processed
        }
        case MotionEvent.ACTION_MOVE: {
            upX = event.getX();
            upY = event.getY();

            float deltaX = downX - upX;
            float deltaY = downY - upY;

            // horizontal swipe detection
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                // left or right
                if (deltaX < 0) {
                   // Logger.show(Log.INFO,logTag, "Swipe Left to Right");
                    mSwipeDetected = Action.LR;
            		Log.d("Foi no","LR");

                    return true;
                }
                if (deltaX > 0) {
                   // Logger.show(Log.INFO,logTag, "Swipe Right to Left");
                    mSwipeDetected = Action.RL;
            		Log.d("Foi no","RL");

                    return true;
                }
            } else 

                // vertical swipe detection
                if (Math.abs(deltaY) > MIN_DISTANCE) {
                    // top or down
                    if (deltaY < 0) {
                     //   Logger.show(Log.INFO,logTag, "Swipe Top to Bottom");
                        mSwipeDetected = Action.TB;
                		Log.d("Foi no","TB");

                        return false;
                    }
                    if (deltaY > 0) {
                       // Logger.show(Log.INFO,logTag, "Swipe Bottom to Top");
                        mSwipeDetected = Action.BT;
                		Log.d("Foi no","BT");
                        return false;
                    }
                } 
            return true;
        }
        }
        return false;
    }
}